package com.cn.jdia.db;

import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;

import org.joda.time.DateTime;

public class DataSync {

	public static void sync(Map<String, String> project, Map<String, Object> memory, List<String> cpu,
			Map<String, Integer> disk) throws FileNotFoundException {

		String serverName = project.get("SERVERNAME");
		String ip = project.get("IP");

		System.out.println(memory.toString());
		System.out.println(cpu.toString());
		System.out.println(disk.toString());

		DBManager4Mysql db = DBManager4Mysql.createInstance();
		db.connectDB();

		String del = "delete from serverdisk where servername='" + serverName + "' and serverip='" + ip + "'";
		int delret = db.executeUpdate(del);
		System.out.println("delete serverdisk count: "+delret);

		String insert = null;
		for(String key:disk.keySet()){
			insert = "insert into serverdisk value('" + serverName + "','" + ip + "','" + key.replace("\\", "\\\\")
					+ "','" + disk.get(key) + "%')";
			System.out.println(insert);
			db.executeUpdate(insert);
		}
		
		del = "delete from serverquota where servername='" + serverName + "' and serverip='" + ip + "'";
		delret = db.executeUpdate(del);
		System.out.println("delete serverquota count :"+delret);
		
		insert = "insert into serverquota value('" + serverName + "','" + ip + "'," + memory.get("total") + ","
				+ memory.get("used") + ",'" + cpu.get(0) + "','" + cpu.get(1) + "','" + cpu.get(2) + "','" + cpu.get(3)
				+ "','" + cpu.get(4) + "')";
		db.executeUpdate(insert);
		
		DateTime dt=new DateTime();
		String time = dt.toString("YYYY-MM-dd HH:mm:ss");
		String update="update serverconfig set bak2='"+time+"' where servername='"+serverName+"' and ip='"+ip+"'";
		db.executeUpdate(update);
		
		insert="insert into serverheartbeat (servername,ip,heartbeat) values ('"+serverName+"','"+ip+"','"+time+"')";
		db.executeUpdate(insert);
		
		db.closeDB();
	}

}
